package com.example.myapplication.ui.activity.coins

import androidx.lifecycle.Observer
import androidx.recyclerview.widget.LinearLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.chad.library.adapter.base.loadmore.SimpleLoadMoreView
import com.example.myapplication.R
import com.example.myapplication.arouter.ARouterPath
import com.example.myapplication.ui.adapter.CoinsRecordAdapter
import com.example.myapplication.ui.adapter.RankAdapter
import com.kun.base.ui.AbsBaseActivity
import com.scwang.smart.refresh.header.ClassicsHeader
import kotlinx.android.synthetic.main.fragment_home.*
import kotlinx.android.synthetic.main.toolbar_layout.*

/**
 *积分流水
 *
 * @author : Kun
 * @date : 2021/6/11
 * @time : 11:52
 */
@Route(path = ARouterPath.USER_COINS)
class UserCoinsActivity : AbsBaseActivity() {
    private var index = 1

    private lateinit var coinsRecordAdapter: CoinsRecordAdapter

    private lateinit var userCoinsViewModel: UserCoinsViewModel

    override fun getLayoutId(): Int {
        return R.layout.activity_user_coins
    }

    override fun initViewModel() {
        userCoinsViewModel = getActivityScopeViewModel(UserCoinsViewModel::class.java)
        userCoinsViewModel.coinsRequest.coinRecordListResult.observe(this, Observer { t ->
            if (t.errorCode == 0) {
                if (t.data!!.curPage == 1) {
                    coinsRecordAdapter.setList(t.data!!.datas)
                } else {
                    coinsRecordAdapter.addData(t.data!!.datas)
                }
                if (t.data!!.curPage >= t.data!!.pageCount) {
                    coinsRecordAdapter.loadMoreModule.loadMoreEnd()
                } else {
                    coinsRecordAdapter.loadMoreModule.loadMoreComplete()
                }
            } else {
                coinsRecordAdapter.loadMoreModule.loadMoreComplete()
            }
            refresh_layout!!.finishRefresh()
        })
    }

    override fun initView() {
        iv_back.setOnClickListener { finish() }
        tv_title.text = getString(R.string.coins_record)

        refresh_layout!!.setRefreshHeader(ClassicsHeader(this))
        coinsRecordAdapter = CoinsRecordAdapter(R.layout.item_coins_record, null)
        coinsRecordAdapter.loadMoreModule.loadMoreView = SimpleLoadMoreView()


        recycler_view!!.layoutManager = LinearLayoutManager(this)
        recycler_view!!.adapter = coinsRecordAdapter

        coinsRecordAdapter.loadMoreModule.setOnLoadMoreListener {
            //加载更多
            index++
            getData()
        }
        refresh_layout!!.setOnRefreshListener {
            //刷新数据
            index = 1
            getData()
        }

        getData()
    }
    private fun getData() {
        userCoinsViewModel.requestCoinRecordList(index)
    }
}